﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmCachDung : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmCachDungEvent;
        int State = 0;

        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmCachDungBusiness dmCachDungBusiness = new DmCachDungBusiness();

        DmCachDungEntity dmCachDungEntity = null;

        public frmCapNhatDmCachDung(int state)
        {
            InitializeComponent();
            State = state;
            txtMaCachDung.Focus();
        }

        public frmCapNhatDmCachDung(int state, DmCachDungEntity obj)
        {
            InitializeComponent();
            State = state;
            dmCachDungEntity = obj;

            txtMaCachDung.Text = dmCachDungEntity.MaCachDung;
            txtTenCachDung.Text = dmCachDungEntity.TenCachDung;
            txtMoTa.Text = dmCachDungEntity.MoTa;
            chkTrangThaiSuDung.Checked = (bool)dmCachDungEntity.TrangThaiSuDung;
            txtMaCachDung.Enabled = false;

            txtTenCachDung.Focus();
        }


        private void frmCapnhatDMCachdung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaCachDung, "Mã cách dùng") == false) return;
            
            dmCachDungEntity = new DmCachDungEntity()
            {
                MaCachDung = txtMaCachDung.Text,
                TenCachDung = txtTenCachDung.Text,
                MoTa = txtMoTa.Text,
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };
            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmCachDungBusiness.GetDmCachDung(txtMaCachDung.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaCachDung, "Mã cách dùng") == false) return;
                
                dmCachDungBusiness.InsertDmCachDung(dmCachDungEntity);
                Validation.ClearControl(this);
                txtMaCachDung.Focus();
            }
            else
            {
                dmCachDungBusiness.EditDmCachDung(dmCachDungEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmCachDungEvent != null)
                dmCachDungEvent(sender, e);
        }


    }
}